package seekting.demo2017.activity;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.HandlerThread;
import android.util.Log;
import android.view.View;

import java.net.URI;
import java.net.URISyntaxException;

import de.tavendo.autobahn.WebSocket;
import de.tavendo.autobahn.WebSocketConnection;
import de.tavendo.autobahn.WebSocketException;
import seekting.demo2017.AppEnv;
import seekting.demo2017.R;

/**
 * Created by seekting on 17-7-24.
 */

public class WebSocketActivity extends Activity {

    private static final String host = "ws://35.160.246.46:8089/ws";
    public static final boolean DEBUG = AppEnv.bAppdebug;
    public static final String TAG = "WebSocketActivity";
    View connect, disconnect;
    private WebSocketConnection mWebSocketConnection;
    private URI mUri;
    private WebSocket.WebSocketConnectionObserver mWebSocketConnectionObserver;
    Handler mHandler;
    HandlerThread mHandlerThread;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.web_socket);

        mWebSocketConnection = new WebSocketConnection();
        connect = findViewById(R.id.connect);
        disconnect = findViewById(R.id.disconnect);
        mWebSocketConnectionObserver = new WebSocket.WebSocketConnectionObserver() {
            @Override
            public void onOpen() {
                if (DEBUG) {
                    Log.d(TAG, "onOpen.");
                }

            }

            @Override
            public void onClose(WebSocketCloseNotification webSocketCloseNotification, String s) {
                if (DEBUG) {
                    Log.d(TAG, "onClose." + s);
                }

            }

            @Override
            public void onTextMessage(String s) {

                if (DEBUG) {
                    Log.d(TAG, "onTextMessage." + s);
                }
            }

            @Override
            public void onRawTextMessage(byte[] bytes) {

                if (DEBUG) {
                    Log.d(TAG, "onRawTextMessage.");
                }
            }

            @Override
            public void onBinaryMessage(byte[] bytes) {

                if (DEBUG) {
                    Log.d(TAG, "onBinaryMessage.");
                }
            }
        };
        try {
            mUri = new URI(host);
        } catch (URISyntaxException e) {
            e.printStackTrace();
        }
        connect.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                connect();

            }
        });
        disconnect.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                disconnect();
            }
        });

    }

    private void disconnect() {
        if (mHandler != null) {
            final HandlerThread temp = mHandlerThread;
            mHandler.post(new Runnable() {
                @Override
                public void run() {
                    mWebSocketConnection.disconnect();
                    temp.quit();
                }
            });
            mHandlerThread = null;
            mHandler = null;
        }
    }

    private void connect() {
        createHandlerIfNeed();
        mHandler.post(new Runnable() {
            @Override
            public void run() {
                try {
                    mWebSocketConnection.connect(mUri, mWebSocketConnectionObserver);
                } catch (WebSocketException e) {
                    e.printStackTrace();
                }
            }
        });
    }

    private void createHandlerIfNeed() {
        if (mHandlerThread == null) {
            mHandlerThread = new HandlerThread(TAG) {
                @Override
                public void run() {
                    super.run();
                    if (DEBUG) {
                        Log.d(TAG, "run.quited()");
                    }
                }
            };
            mHandlerThread.start();
            mHandler = new Handler(mHandlerThread.getLooper());
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        disconnect();
    }
}
